package v1

import (
	"github.com/gin-gonic/gin"
	"go-admin/global"
	mr "go-admin/internal/model/api/model_request"
	ms "go-admin/internal/model/api/model_response"
	"go-admin/internal/service"
	"go-admin/pkg/app"
	"go-admin/pkg/errcode"
)

// 字典项

type DictItem struct {
}

func NewDictItem() DictItem {
	return DictItem{}
}

// 创建
func (d *DictItem) Create(c *gin.Context) {
	param := mr.CreateDictItemRequest{}
	response := app.NewResponse(c)
	valid, errors := app.BindAndValid(c, &param)
	if !valid {
		// 打印日志
		global.Logger.Errorf("app.BindAndValid errs: %v", errors)
		response.Fail(app.WithMsg(errors.Errors()...))
		return
	}
	svc := service.New(c.Request.Context())
	insertId, err := svc.CreateDictItem(&param)
	if err != nil {
		global.Logger.WithCaller(2).Errorf("svc.CreateDictItem err: %v", err)
		response.Fail(app.WithMsg(errcode.ErrCreate))
		return
	}
	response.Ok(app.WithData(app.ToId(insertId)))
	return
}

// 获取列表
func (d *DictItem) List(c *gin.Context) {
	param := mr.ListDictItemRequest{}
	response := app.NewResponse(c)
	valid, errors := app.BindAndValid(c, &param)
	if !valid {
		// 打印日志
		global.Logger.Errorf("app.BindAndValid errs: %v", errors)
		response.Fail(app.WithMsg(errors.Errors()...))
		return
	}
	svc := service.New(c.Request.Context())

	// 获取列表总数
	totalRows, err := svc.CountDictItem(&param)
	if err != nil {
		// 打印日志
		global.Logger.WithCaller(2).Errorf("svc.CountDictItem errs: %v", errors)
		response.Fail(app.WithMsg(errors.Errors()...))
		return
	}
	// 获取分页信息
	pager := app.Pager{PageIndex: app.GetPageIndex(c), PageSize: app.GetPageSize(c), TotalRows: totalRows}
	list, err := svc.GetDictListItem(&param, &pager)
	if err != nil {
		// 打印日志
		global.Logger.WithCaller(2).Errorf("svc.CountDict errs: %v", err)
		response.Fail(app.WithMsg(errcode.ErrList))
		return
	}
	response.Ok(app.WithData(app.ToPageList(pager, ms.ModelToListDictItemRes(list))))
	return
}

func (d *DictItem) DictItemDetailById(c *gin.Context) {
	param := mr.CommonId{}
	response := app.NewResponse(c)
	valid, errors := app.BindAndValid(c, &param)
	if !valid {
		// 打印日志
		global.Logger.WithCaller(2).Errorf("app.BindAndValid errs: %v", errors)
		response.Fail(app.WithMsg(errors.Errors()...))
		return
	}
	svc := service.New(c.Request.Context())
	dictItem, err := svc.GetDictItemById(param.Id)
	if err != nil {
		// 打印日志
		global.Logger.WithCaller(2).Errorf("app.GetDictById errs: %v", err)
		response.Fail(app.WithMsg(errcode.ErrGet))
		return
	}
	response.Ok(app.WithData(app.ToDetail(ms.ModelToDetailDictItemRes(dictItem))))
}

// 修改字典项
func (d *DictItem) Update(c *gin.Context) {
	param := mr.UpdateDictItemRequest{}
	response := app.NewResponse(c)
	valid, errors := app.BindAndValid(c, &param)
	if !valid {
		// 打印日志
		global.Logger.Errorf("app.BindAndValid errs: %v", errors)
		response.Fail(app.WithMsg(errors.Errors()...))
		return
	}
	svc := service.New(c.Request.Context())
	if err := svc.UpdateDictItem(&param); err != nil {
		global.Logger.WithCaller(2).Errorf("svc.UpdateDictItem err: %v", err)
		response.Fail(app.WithMsg(errcode.ErrEdit))
		return
	}
	response.Ok()
	return
}

// 批量删除
func (d *DictItem) Deletes(c *gin.Context) {
	param := mr.CommonIds{}
	response := app.NewResponse(c)
	valid, errors := app.BindAndValid(c, &param)
	if !valid {
		// 打印日志
		global.Logger.Errorf("app.BindAndValid errs: %v", errors)
		response.Fail(app.WithMsg(errors.Errors()...))
		return
	}
	svc := service.New(c.Request.Context())
	if err := svc.DeleteDictItemS(param.Ids); err != nil {
		// 打印日志
		global.Logger.WithCaller(2).Errorf("svc.DeleteDictItemS errs: %v", errors)
		response.Fail(app.WithMsg(errcode.ErrDelete))
		return
	}
	response.Ok()
	return
}

// 根据字典类型获取字典选项
func (d *DictItem) DictItemList(c *gin.Context) {
	param := mr.DictItemListRequest{}
	response := app.NewResponse(c)
	valid, errors := app.BindAndValid(c, &param)
	if !valid {
		// 打印日志
		global.Logger.Errorf("app.BindAndValid errs: %v", errors)
		response.Fail(app.WithMsg(errors.Errors()...))
		return
	}
	svc := service.New(c.Request.Context())
	dictItemList, err := svc.DictItemList(&param)
	if err != nil { // 打印日志
		global.Logger.WithCaller(2).Errorf("svc.DictItemList errs: %v", errors)
		response.Fail(app.WithMsg(errcode.ErrGet))
		return
	}
	response.Ok(app.WithData(app.ToList(ms.ModelToDictItemKVRes(dictItemList))))
	return
}
